<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">
        <h2>PCB ERP管理系统</h2>
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="0px">
        <el-form-item prop="employeeCode">
          <el-input 
            v-model="loginForm.employeeCode" 
            placeholder="员工代码" 
            prefix-icon="User"
            @keyup.enter="handleLogin">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="密码" 
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleLogin" 
            style="width: 100%">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '../api/auth'

export default {
  name: 'Login',
  components: {
    User,
    Lock
  },
  setup() {
    const router = useRouter()
    const loginFormRef = ref(null)
    const loading = ref(false)
    
    const loginForm = reactive({
      employeeCode: '',
      password: ''
    })
    
    const loginRules = {
      employeeCode: [
        { required: true, message: '请输入员工代码', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    const handleLogin = () => {
      loginFormRef.value.validate(valid => {
        if (valid) {
          loading.value = true
          
          login(loginForm)
            .then(response => {
              const { code, data, message } = response.data
              
              if (code === 200) {
                // 存储登录信息
                localStorage.setItem('token', data.token)
                localStorage.setItem('userInfo', JSON.stringify({
                  id: data.id,
                  employeeCode: data.employeeCode,
                  employeeName: data.employeeName,
                  role: data.role
                }))
                
                ElMessage({
                  message: '登录成功',
                  type: 'success'
                })
                
                // 跳转到首页
                router.push('/')
              } else {
                ElMessage.error(message || '登录失败')
              }
            })
            .catch(error => {
              console.error('登录请求出错:', error)
              ElMessage.error('登录失败，请稍后重试')
            })
            .finally(() => {
              loading.value = false
            })
        }
      })
    }
    
    return {
      loginForm,
      loginRules,
      loginFormRef,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 350px;
  padding: 30px;
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
}

.login-title h2 {
  font-weight: bold;
  color: #409EFF;
}
</style> 